package com.app.order;

import com.app.common.entity.Order;
import com.app.common.service.AccountService;
import com.app.common.service.OrderService;
import com.xhga.transaction.common.entity.TransactionLog;
import com.xhga.transaction.common.service.TransactionLogService;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;


/**
 * 测试 dubbo 服务
 *
 * @author xhga
 * @since 2019-04-03
 */
@RestController
@RequestMapping("/order")
public class OrderController {
    @Autowired
    private OrderService orderService;
    @Autowired
    private AccountService accountService;
    @RequestMapping("/distributed")
    @ResponseBody
    public String distributedTransaction(@Param("userId") int userId,
                                         @Param("goodsId") int goodsId,
                                         @Param("count") int count) {
        try {
            System.out.println("线程名字web:"+Thread.currentThread().getName());
            String centreNo = "test";
            int money = 1;
            accountService.updateMoney(userId, money);
            Order order = new Order();
            order.setOrderNo(centreNo);
            order.setOrderMoney((double) money);
            order.setOrderDate(new Date());
            order.setOrderGoodsName("goods name.");
            order.setUserId(userId);
            // 第二步(订单)：业务系统操作->生成订单
            orderService.addOrder(order);
        } catch (Exception e) {
            System.out.println("操作异常 或 服务异常");
        }
        return "完成操作.";
    }

    /*-----------------------------别看了,有鬼----------------------------*/
    /**
     * 测试rpc服务
     * @return
     */
    @RequestMapping("/add")
    @ResponseBody
    public String add() {
        Order order = new Order();
        order.setId(1);
        order.setOrderDate(new Date());
        order.setOrderGoodsName("app test");
        order.setOrderMoney(1.0);
        order.setOrderNo("test");
        order.setUserId(1);
        order.setVersion(1);
        try {
            orderService.addOrder(order);
        } catch (Exception e) {
            System.out.println("订单创建异常.");
        }
        return "操作.";
    }
}
